clear 
global rawdata "data directory here" 
global output "output directory here"

cd $rawdata
use korea_data2.dta
gen xx = ID_2/10

generate splitat = ustrpos(NL_NAME_2, "|")
generate str1 name = ""
replace name = usubstr(NL_NAME_2,1, splitat-1)

generate str1 name2 = ""
generate splitat2 = ustrpos(NL_NAME_2, "") 
replace name2 = usubstr(NL_NAME_2,1, splitat2-1) if splitat2>0

gen statetext = ""
replace statetext = "서울특별시" if NAME_1 == "Seoul"
replace statetext = "부산광역시" if NAME_1 == "Busan"
replace statetext = "충청북도" if NAME_1 == "Chungcheongbuk-do"
replace statetext = "충청남도" if NAME_1 == "Chungcheongnam-do"
replace statetext = "대구광역시" if NAME_1 == "Daegu"
replace statetext = "대전광역시" if NAME_1 == "Daejeon"
replace statetext = "강원도" if NAME_1 == "Gangwon-do"
replace statetext = "광주광역시" if NAME_1 == "Gwangju"
replace statetext = "경기도" if NAME_1 == "Gyeonggi-do"
replace statetext = "경상북도" if NAME_1 == "Gyeongsangbuk-do"
replace statetext = "경상남도" if NAME_1 == "Gyeongsangnam-do"
replace statetext = "인천광역시" if NAME_1 == "Incheon"
replace statetext = "제주도" if NAME_1 == "Jeju"
replace statetext = "전라북도" if NAME_1 == "Jeollabuk-do"
replace statetext = "전라남도" if NAME_1 == "Jeollanam-do"
replace statetext = "세종시" if NAME_1 == "Sejong"
replace statetext = "울산광역시" if NAME_1 == "Ulsan"

gen geotext = name
gen geotext_short = usubstr(geotext,1,2)
keep geotext geotext_short statetext name ID_2 NAME_1 NAME_2

cd $output
save mapdata.dta,replace

 
clear
cd $output
use geo_bankshock.dta
drop if dbanks == .
rename geo geocode2000
merge m:1 geo using 2000geo.dta
drop _merge

gen geotext_short = usubstr(geotext,1,2)
merge m:m statetext geotext_short using mapdata.dta

sort ID_2
bys ID_2: gen  ID_2_new = [_n]
bys ID_2: egen dbanks_mean = mean(dbanks)
keep if ID_2_new == 1
drop ID_2_new
replace dbanks = dbanks_mean

cd $rawdata 
spmap dbanks using korea_coordinates2, id(ID_2) fcolor(Blues) ndfcolor(gray) 

 